/**
 * Created with JetBrains WebStorm.
 * User: huwei
 * Date: 13-8-22
 * Time: 下午1:09
 * To change this template use File | Settings | File Templates.
 */
Ext.ux.WindowAlwaysOnTop = function(){
    this.init = function(win){
        win.on('deactivate', function(){
            var i=1;
            this.manager.each(function(){i++});
            this.setZIndex(this.manager.zseed + (i*10));
        })
    }
}

function showHandlerWin(url,title,width,height){
    if(typeof(width) == 'undefined'){
        width = 900;
    }
    if(typeof(height) == 'undefined'){
        height = 600;
    }
    win = null;
    if (!win) {
        win = new Ext.Window({
            title: title,
            header: {
                titlePosition: 2,
                titleAlign: 'center'
            },
            closable: true,
            closeAction: 'hide',
            x : 0,
            y : 0,
            width: width,
            height: height,
            html : '<iframe width="100%" height="100%" frameborder="0" src="' + url + '"></iframe>'
        });
    }
    win.show();

}
function doEdit(grid,_url,title,width,height) {
    var orows = grid.getSelectionModel().getSelection();
    if(orows.length >1 || orows.length == 0){
        Ext.MessageBox.show({title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    var url = "";
    if(_url.indexOf("?")>=0){
        url = _url + "&_id="  + orows[0].data._id;
    }
    else{
        url = _url + "?_id=" + orows[0].data._id;
    }
    showHandlerWin(url,title,width,height);
}

function doAuth(grid,_url,title,width,height) {
    var orows = grid.getSelectionModel().getSelection();
    if(orows.length >1 || orows.length == 0){
        Ext.MessageBox.show({title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    var url = "";
    if(_url.indexOf("?")>=0){
        url = _url + "&_id="  + orows[0].data._id;
    }
    else{
        url = _url + "?_id=" + orows[0].data._id;
    }
    showHandlerWin(url,title,width,height);
}

function openWin(grid,_url,title,width,height) {
    var orows = grid.getSelectionModel().getSelection();
    if(orows.length >1 || orows.length == 0){
        Ext.MessageBox.show({title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    var url = "";
    if(_url.indexOf("?")>=0){
        url = _url + "&_id="  + orows[0].data._id;
    }
    else{
        url = _url + "?_id=" + orows[0].data._id;
    }
    showHandlerWin(url,title,width,height);
}


function doAdd(url,title,width,height){
    showHandlerWin(url,title,width,height);
}

function doDel(grid,url,store) {
    var orows = grid.getSelectionModel().getSelection();
    if(orows.length > 1 || orows.length == 0){
        Ext.MessageBox.show({title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    Ext.MessageBox.confirm('消息提示', '确定删除对象？', function (btn) {
        if (btn == "yes") {
            Ext.Ajax.request({
                url: url,
                params: { _id: orows[0].get('_id') },
                method: 'POST',
                success: function (response, options) {
                    var data = Ext.decode(response.responseText);
                    if(data.flag == "success") {
                        Ext.MessageBox.alert('消息提示', '操作成功！');
                        store.loadPage(1);
                        window.parent.leftFrame.location.reload();
                    }else {
                        Ext.MessageBox.alert('消息提示', data.msg);
                    }
                },
                failure: function (response, options) {
                    Ext.MessageBox.alert('消息提示', '操作失败！');
                }
            });
        }
    })

}


function doDelete(grid,url,store) {
    var orows = grid.getSelectionModel().getSelection();
    if(orows.length > 1 || orows.length == 0){
        Ext.MessageBox.show({title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    Ext.MessageBox.confirm('消息提示', '确定删除对象？', function (btn) {
        if (btn == "yes") {
            Ext.Ajax.request({
                url: url,
                params: { _id: orows[0].get('_id') },
                method: 'POST',
                success: function (response, options) {
                     Ext.MessageBox.alert('消息提示', '操作成功！');
                     store.loadPage(1);

                },
                failure: function (response, options) {
                    Ext.MessageBox.alert('消息提示', Ext.JSON.decode(response.responseText).error);
                }
            });
        }
    })

}

function uploadFile(_form,filename,image_ctl) {
    var url = "/file/upload";
    if (!_form.getForm().findField("attach_file").getValue()) {
        Ext.Msg.alert('消息提示', '请浏览需要上传的文件!');
        return;
    }
    if(filename){
        url += '?filename=' + filename;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function (form, action) {
            _form.getForm().findField("image_file_id").setValue(action.result.file_id);
            if(image_ctl){
                if(filename){
                    image_ctl.setSrc('/_image/' + filename);
                }
                else{
                    image_ctl.setSrc('/_image/' + action.result.file_id);
                }

            }
            Ext.Msg.alert('消息提示', '上传成功，请按保存继续操作！');
        },
        failure: function (form, action) {
            if(action && action.response && action.response.responseText){
                Ext.Msg.alert('消息提示', Ext.JSON.decode(action.response.responseText).error);
            }
            else{
                Ext.Msg.alert('消息提示', '上传失败！');
            }

        }
    });
}

function resizeImage(srcfile,width,filename){
    Ext.Ajax.request({
        url: '/img_handler/resize',
        params: { srcfile:srcfile,width:width,filename:filename},
        method: 'get',
        success: function (response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标成功！');
        },
        failure: function (response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标失败！');
        }
    });
}


